package mul.Logic;

import mul.UI.R;
import mul.UI.SoundManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Sprite{
	protected Canvas canvas;
	protected Bitmap animation;
	protected float xpos;
	protected float ypos;
	protected Rect rectangle;
	protected int fps;
	protected int numberOfFrames;
	protected int currentFrame;
	protected long frameTimer;
	protected int spriteHeight;
	protected int spriteWidth;
	protected Paint paint;
	protected int bitMapAdjustTop;
	protected int bitMapAdjustLeft;
	protected Resources resources;
	protected boolean loop;
	public boolean dispose;
	
	public Sprite(float xpos, float ypos, int bitmapAdjustTop, int bitmapAdjustLeft){
		this.rectangle = new Rect(0,0,0,0);
		this.frameTimer = 0;
		this.currentFrame = 0;
		this.xpos = xpos;
		this.ypos = ypos;
		this.bitMapAdjustTop = bitmapAdjustTop;
		this.bitMapAdjustLeft = bitmapAdjustLeft;
	}
	
	public void Initalize(int height, int width, int fps, int frameCount, Bitmap animation, boolean loop){
		this.spriteHeight = height;
		this.spriteWidth = width;
		this.rectangle.top = this.bitMapAdjustTop;
		this.rectangle.bottom = this.bitMapAdjustTop+this.spriteHeight;
		this.rectangle.left = this.bitMapAdjustLeft;
		this.rectangle.right = this.bitMapAdjustLeft+this.spriteWidth;
		this.fps = 1000/fps;
		this.numberOfFrames = frameCount;
		this.animation = animation;
	}
	
	public void update(long gameTime){
		
		if(gameTime > this.frameTimer + this.fps){
			this.frameTimer = gameTime;
			this.currentFrame += 1;
			
			if(this.currentFrame >= this.numberOfFrames){
				this.currentFrame = 0;
				if(!this.loop){
					this.dispose = true;
				}
			}
		}
		
		this.rectangle.left = this.currentFrame * this.spriteWidth;
		this.rectangle.right = this.rectangle.left + this.spriteWidth;
	}
	public void draw(Canvas canvas){
		Rect destination = new Rect((int)this.getXpos(), (int)this.getYpos(), (int)(this.getXpos()+this.spriteWidth), (int)(this.getYpos()+this.spriteHeight));
		canvas.drawBitmap(this.animation, this.rectangle, destination, this.paint);
		if(this.currentFrame == this.numberOfFrames)return;
	}
	
	public float getXpos(){
		return this.xpos;
	}
	
	public float getYpos(){
		return this.ypos;
	}
}
